Page({
 data: {
  // 类别数据
  categoryList: [],
  // 请求类别携带的数据
  topCategoryCode: [],
  // loading
  loading: true,
  // 专区名称
  ProductAreaName: '',
  // 专区ID
  ProductAreaId: ''
 },
 // 静态数据
 static: {
  // 请求的页码值
  page: 1,
  // 用于分辨是专区的请求还是tab的
  code: true,
 },
 // 获取类别列表 tab
 getTopCategoryCode() {
  wx.request({
   url: 'https://starmid.minutestars.com/api/product4color/getPageProductsByTopCategory',
   method: "GET",
   data: {
    "topCategoryCode": this.data.topCategoryCode,
    "pageIndex": this.static.page,
    "pageSize": 8,
    "tableView": "CategoryProductsView4Color",
    "specialId": undefined
   },
   success: (res) => {
    // console.log(res);
    let { Result, Data } = res.data
    if (Result.Success) {
     Data.products.forEach((item, index) => {
      if (item.promoteDiscount) {
       item.promoteDiscount = Math.fround(item.price * item.promoteDiscount).toFixed(2)
      }
     })
     this.setData({
      categoryList: [...this.data.categoryList, ...Data.products],
      loading: this.data.loading = true
     })
     wx.hideLoading()
    }
   }
  })
 },
 // 专区请求
 getZoneData() {
  let url = ''
  let data = {}
  // 判断 id 是否为 0 是则表示是新品福利的请求
  if (this.data.ProductAreaId !== '0') {
   url = 'https://starmid.minutestars.com/api/product4color/getPageProducts'
   data = {
    "topCategoryCode": '',
    "pageIndex": this.static.page,
    "pageSize": 8,
    "tableView": "ShowAreaItemProductsView4Color",
    "specialId": this.data.ProductAreaId,
    "specialTopic": '',
    "searchWord": '',
   }
  } else {
   // 否则则是 今日秒杀、清仓特卖、全部服饰的请求
   url = 'https://starmid.minutestars.com/api/product4color/getPageProducts4Type'
   data = {
    "topCategoryCode": undefined,
    "pageIndex": this.static.page,
    "pageSize": 8,
    "tableView": "None",
    "specialId": this.data.ProductAreaId,
    "specialTopic": '',
    "searchWord": '',
    "type": 'newarrival',
    "inDays": 30,
   }
  }
  // 发起请求
  wx.request({
   url: url,
   method: "GET",
   data: data,
   success: (res) => {
    // console.log(res);
    let { Result, Data } = res.data
    if (Result.Success) {
     Data.products.forEach((item, index) => {
      if (item.promoteDiscount) {
       item.promoteDiscount = Math.fround(item.price * item.promoteDiscount).toFixed(2)
      }
     })
     this.setData({
      categoryList: [...this.data.categoryList, ...Data.products]
     })
     wx.hideLoading()
    }
   }
  })
 },
 onLoad(options) {
  let { topCategoryCode, title, ProductAreaName, ProductAreaId, code } = options
  // 判断code 是否为 zone 是则表示是专区的请求
  console.log(ProductAreaId);
  if (code === 'zone') {
   this.static.code = false
   wx.setNavigationBarTitle({ title: ProductAreaName })
   this.setData({ ProductAreaName, ProductAreaId })
   this.getZoneData()
   wx.showLoading({
    title: '加载中...',
   })
   // 判断是否为 商品详情的请求
  } else {
   // 否则则是 tab的请求
   this.setData({ topCategoryCode: topCategoryCode })
   wx.setNavigationBarTitle({ title })
   this.getTopCategoryCode()
   wx.showLoading({
    title: '加载中...',
   })
  }
 },
 /**
  * 页面上拉触底事件的处理函数
  */
 onReachBottom() {
  this.setData({
   loading: this.data.loading = false
  })
  // console.log(this.static.code);
  this.static.page++
  if (this.static.code) {
   this.getTopCategoryCode()
  } else {
   this.getZoneData()
  }
 },
})